package com.epam.edu.command;

import com.epam.edu.constant.*;
import com.epam.edu.dao.*;
import com.epam.edu.db.ConnectionPool;
import com.epam.edu.entity.*;
import com.epam.edu.util.Parser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class OrderViewCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, NoSuchAlgorithmException {
        ConnectionPool pool = null;
        Connection connection = null;
        try {
            ConnectionPool.init();
            pool = ConnectionPool.getInstance();
            connection = pool.takeConnection();
            Order order = null;
            HttpSession session = request.getSession();
            int userId = Integer.parseInt(session.getAttribute("userId").toString());
            int orderId = 0;
            if ((DAOUser.selectById(connection, userId).getRole() == Role.ADMIN)) {    //if user is ADMIN
                String[] ordersId = request.getParameterValues("radio");
                orderId = Integer.parseInt(ordersId[0]);
                order = DAOOrder.selectById(connection, orderId);
            } else {
                order = DAOOrder.selectByUserId(connection, userId);
                orderId = order.getId();
            }
            request.setAttribute("order", order);
            EntrantsData entrantsData = DAOEntrantsData.selectByOrderID(connection, orderId);
            request.setAttribute("entrantsData", entrantsData);
            List<Nationality> nationalities = DAONationality.selectAll(connection);
            request.setAttribute("nationalities", nationalities);
            List<String> sexs = new LinkedList<String>();
            for (Sex sex : Sex.values()) {
                sexs.add(sex.toString());
            }
            request.setAttribute("sexs", sexs);
            List<String> familyStatues = new LinkedList<String>();
            for (FamilyStatus familyStatus : FamilyStatus.values()) {
                familyStatues.add(familyStatus.toString());
            }
            request.setAttribute("familyStatues", familyStatues);
            List<Citizenship> citizenship = DAOCitizenship.selectAll(connection);
            request.setAttribute("citizenship", citizenship);
            List<DocumentType> docTypes = DAODocumentType.selectAll(connection);
            request.setAttribute("documentTypes", docTypes);
            List<Department> departments = DAODepartment.selectAll(connection);
            request.setAttribute("departments", departments);
            if (!"".equals(entrantsData.getPhones())&& entrantsData.getPhones()!=null) {
                String[] phones = Parser.dividedIntoTwoParts(entrantsData.getPhones(), ';');
                request.setAttribute("phone1", phones[0]);
                request.setAttribute("phone2", phones[1]);
            } else {
                request.setAttribute("phone1", "");
                request.setAttribute("phone2", "");
            }
            if (!"".equals(entrantsData.getEducationYears())&& entrantsData.getEducationYears()!=null) {
                String[] years = Parser.dividedIntoTwoParts(entrantsData.getEducationYears(), ';');
                request.setAttribute("yearBegin", years[0]);
                request.setAttribute("yearEnd", years[1]);
            } else {
                request.setAttribute("yearBegin", "");
                request.setAttribute("yearEnd", "");
            }
            OrdersMarks ordersMarks = DAOOrdersMarks.selectByOrderId(connection, orderId);
            List<Specialty> specialties = DAOSpecialty.selectByMarks(connection, ordersMarks, order.getLanguage(),
                    order.getTeachingForm(), order.getTeachingPlan());
            request.setAttribute("specialties", specialties);
            List<TeachingForm> teachingForms = DAOTeachingForm.selectByMarks(connection, ordersMarks, order.getLanguage(),
                    order.getTeachingPlan());
            request.setAttribute("teachingForms", teachingForms);
            TeachingPlan teachingPlans = entrantsData.getEducationType().getTeachingPlan();
            request.setAttribute("teachingPlans", teachingPlans);
            List<String> languages = new LinkedList<String>();
            for (Language lang : Language.values()) {
                languages.add(lang.toString());
            }
            request.setAttribute("languages", languages);
            List<String> paymentTypes = new LinkedList<String>();
            for (PaymentType pay : PaymentType.values()) {
                paymentTypes.add(pay.toString());
            }
            request.setAttribute("paymentTypes", paymentTypes);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "WEB-INF/jsp/order.jsp";
    }
}
